﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CartItemService.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the CartItemService type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Services
{
  using System;
  using System.Collections.Generic;
  using System.Linq;

  using BusinessEngineInterfaces;
  using Entities;
  using Exceptions;
  using ServiceInterfaces;
  using UnityRepository.Interfaces;

  public class CartItemService : ICartItemService
  {
    private readonly ICartItemEngine cartItemEngine;
    private readonly ICartItemRepository cartItemRepository;
    private readonly ICheckOutRepository checkOutRepository;
    private readonly IDocumentRepository documentRepository;

    public CartItemService(ICartItemEngine cartItemEngine, ICartItemRepository cartItemRepository, ICheckOutRepository checkOutRepository, IDocumentRepository documentRepository)
    {
      this.cartItemEngine = cartItemEngine;
      this.cartItemRepository = cartItemRepository;
      this.checkOutRepository = checkOutRepository;
      this.documentRepository = documentRepository;
    }

    public AddToCartResults AddGrid(String userName, String cartId, String grid)
    {
      return this.cartItemEngine.AddGrid(userName, cartId, grid);
    }

    public AddToCartResults AddItems(String userName, String cartId, IEnumerable<String> documentIds)
    {
      return this.cartItemEngine.AddItems(userName, cartId, documentIds);
    }

    public void RemoveItem(String documentId, String cartId)
    {
      CartItem cartItem;
      CheckOut checkOut;
      try
      {
        cartItem = this.cartItemRepository.GetCart(documentId, cartId);
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to retrieve cart item", e);
      }

      try
      {
        checkOut = this.checkOutRepository.GetCheckOut(cartItem.Document.DocumentId);
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to get check out", e);
      }

      try
      {
        this.cartItemRepository.Delete(cartItem);
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to delete cart item", e);
      }

      try
      {
        this.checkOutRepository.Delete(checkOut);
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to delete check out", e);
      }
    }

    public IList<CartItem> GetCart(String cartId)
    {
      try
      {
        return this.cartItemRepository.GetCart(cartId);
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to get cart items", e);
      }
    }

    public Int32 GetNumberOfItemsInCart(String cartId)
    {
      try
      {
        return this.cartItemRepository.GetNumberOfItemsInCart(cartId);
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to get the numnber cart items", e);
      }
    }

    public void ClearCart(String cartId)
    {
      try
      {
        List<CartItem> cartItems = this.cartItemRepository.GetCart(cartId);
        List<CheckOut> checkOuts = this.checkOutRepository.GetCheckOutsForCart(cartId);

        this.cartItemRepository.Delete(cartItems);
        this.checkOutRepository.Delete(checkOuts);
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to clear cart", e);
      }
    }

    public PagedResult<CartItem> GetCartItems(String cartId, Int32 pageNumber, Int32 numberOfItems)
    {
      try
      {
        return this.cartItemRepository.GetCart(cartId, pageNumber, numberOfItems);
      }
      catch (Exception e)
      {
        throw new UnityException("Unable to get cart items", e);
      }
    }
  }
}
